package com.zzuli.fitness.controller;

import com.zzuli.fitness.entity.Coach;
import com.zzuli.fitness.entity.Course;
import com.zzuli.fitness.entity.CourseVo;
import com.zzuli.fitness.entity.GymInfo;
import com.zzuli.fitness.service.CoachService;
import com.zzuli.fitness.service.CourseService;
import com.zzuli.fitness.service.GymService;
import com.zzuli.fitness.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "课程接口")
@RestController
@RequestMapping("/course")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private CoachService coachService;

    @Autowired
    private GymService gymService;

    @ApiOperation(value = "当天团课列表")
    @GetMapping("/findCourseOneDay")
    public Result findCourseOneDay(@RequestParam String day) {
        List<CourseVo> courseInfoList = new ArrayList<>();
        List<Course> list = courseService.findCourseOneDay(day);
        for (int i=0;i<list.size();i++){
            Course course = list.get(i);
            Coach coach = coachService.getById(course.getCoachId());
            CourseVo courseVo = new CourseVo();
            courseVo.setId(course.getId());
            courseVo.setName(course.getName());
            courseVo.setInfo(course.getInfo());
            courseVo.setPrice(course.getPrice());
            String timeSlot = course.getBeginTime() + "-" + course.getEndTime();
            courseVo.setTime(timeSlot);
            String courseImg = "http://127.0.0.1:8090/fitness" + course.getImage();
            courseVo.setImage(courseImg);
            courseVo.setKeyword(course.getKeyword());
            courseVo.setCoach(coach.getName());
            courseVo.setAmount(course.getAmount());
            GymInfo gymInfo = gymService.getById(1);
            courseVo.setAddress(gymInfo.getAddress());
            String coachImg = "http://127.0.0.1:8090/fitness" + coach.getImage();
            courseVo.setCoachImage(coachImg);
            courseVo.setCoachId(course.getCoachId());
            courseInfoList.add(courseVo);
        }
        return Result.ok(courseInfoList);
    }

    @ApiOperation(value = "专题课程列表")
    @GetMapping("/findSpecialCourse")
    public Result findSpecialCourse() {
        List<Course> list = courseService.selectSpecialCourse();
        for (int i=0;i<list.size();i++){
            Course course = list.get(i);
            String url = "http://127.0.0.1:8090/fitness" + course.getImage();
            course.setImage(url);
        }
        return Result.ok(list);
    }

    @ApiOperation(value = "私教定制课程")
    @GetMapping("/findPrivateCourse")
    public Result findPrivateCourse(@RequestParam Integer id) {
        Course course = courseService.selectPrivateCourse(id);
        String url = "http://127.0.0.1:8090/fitness" + course.getImage();
        course.setImage(url);
        return Result.ok(course);
    }

    @ApiOperation(value = "课程信息")
    @GetMapping("/findCourseInfo")
    public Result findCourseInfo(@RequestParam Integer id) {
        Course course = courseService.getById(id);
        String url = "http://127.0.0.1:8090/fitness" + course.getImage();
        course.setImage(url);
        return Result.ok(course);
    }

    @ApiOperation(value = "课程预约状态")
    @GetMapping("/findCourseStatus")
    public Result findCourseStatus(@RequestParam Integer id) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
        Course course = courseService.getById(id);
        String time = course.getDay() + " " + course.getBeginTime();
        //课程开始时间
        Date date1 = sdf.parse(time);
        //系统当前时间
        Date date2 = sdf.parse(sdf.format(new Date()));
        if (date2.after(date1)) {
            return Result.ok(1);
        }else if(course.getAmount()==0) {
            return Result.ok(2);
        }else {
            return Result.ok(0);
        }
    }
}
